using System;
using System.Reflection;
using Jandaya.Framework.DAO;
using Jandaya.Framework.Core.Empresa;

namespace Jandaya.Framework.Application
{
	public class AppManager
	{
		private ObjectManager _objects = null;
		private ConnectionManager _connections = null;
		private Configuration _config = null;
		
		private Empresa _empresa = null;		
		
		public AppManager(){
			_objects = new ObjectManager();
			_connections = new ConnectionManager();
			_config = new Configuration();
		}
	
		
		public JandayaObject GetNew(Type pObjectType){
			JandayaObject result = 	(JandayaObject)Activator.CreateInstance(pObjectType);
			result.Manager = this;
			return result;
		}
		
		
		/// <summary>
		/// Chache de objetos no nível da aplicação
		/// </summary>
		public ObjectManager Objects{ 
			get {
				return _objects;
			}
		}		
		
		/// <summary>
		/// Gerenciador de conexões
		/// </summary>
		public ConnectionManager Connections{ 
			get { 
				return _connections; 
			}
		}		
		
		public Configuration Config{
			get {
				return _config;
			}
		}
		
						
		public bool LoadConfiguration(){
			bool result = _config.LoadConfiguration();
			if (result){				
				Connections.Database = _config.DBInfo.FullDatabasePath;
				Connections.User = _config.DBInfo.UserName;
				Connections.Password = _config.DBInfo.Password;
			}
			return result;
		}
		
		
		public Empresa Empresa {
			get {
				if ( _empresa == null )
					_empresa = (Empresa)GetNew(typeof(Empresa));
				return _empresa;
			}
			set{				
				_empresa = value;
			}
		}
		
	}
}

